/**
 * Created by zhulizhe on 2017/8/16.
 */
import React, {PureComponent, Component} from 'react';

import {StackNavigator, TabNavigator} from 'react-navigation';
import {
    Image,
    StatusBar,
    DeviceEventEmitter
} from 'react-native';
import Home from './home/Home'
import DeliveryAddress from './address/DeliveryAddress'
import Map from './address/Map'
import Order from './order/Order'
import OrderDetailController from './order/OrderDetail'
import Me from './me/Me'
import PersonInfo from './me/PersonInfo'
import Setting from './me/Setting'
import AccountMgr from './me/AccountMgr'
import ChangePwd from './me/ChangePwd'
import WaterShop from './home/WaterShop'
import TicketDetails from './home/TicketDetails'
import ProductDetails from './home/ProductDetails'
import Brand from './home/Brand'
import Constant from "./Contant";
import AddOrEditAddress from "./address/AddOrEditAddress";
import SearchAddress from "./address/SearchAddress";
import MineWaterTikets from './me/WaterTiket/MineWaterTikets'
import ConfirmOrder from './home/ConfirmOrder'
import QuickPay from './home/QuickPay'
import Login from './login/Login'
import TiketsViable from './home/TiketsViable'
import AddressList from './address/AddressList'
import Judge from './order/Judge'
import CityChooser from './address/CityChooser'
import PayResult from './home/PayResult'
import ShoppingCart from './home/ShoppingCart'
import RequestUtil from "./util/NetUtil";
import loginStore from "./store/LoginStore";


global.LOG = (...args) => {
    // console.log('**********************');
    // console.log(...args);
    // console.log('**********************');
    return args[args.length - 1];
};
const TabNav = TabNavigator(
    {
        MainTab: {
            screen: Home,
            path: 'root/home',
            navigationOptions: {
                tabBarLabel: '首页',
                tabBarIcon: ({tintColor, focused}) => (
                    <Image
                        source={require('../asserts/tab/home-page-click-icon.png')}
                        style={[{tintColor: tintColor}]}
                    />
                ),
            },
        },
        OrderTab: {
            screen: Order,
            path: '/',
            navigationOptions: {
                title: '订单',
                tabBarIcon: ({tintColor, focused}) => (
                    <Image
                        source={require('../asserts/tab/order-click.png')}
                        style={[{tintColor: tintColor}]}
                    />
                ),
            },
        },
        MeTab: {
            screen: Me,
            path: '/',
            navigationOptions: {
                title: '我的',
                tabBarIcon: ({tintColor, focused}) => (
                    <Image
                        source={require('../asserts/tab/mine-click-icon.png')}
                        style={[{tintColor: tintColor}]}
                    />
                ),
            },
        },
    },
    {
        tabBarPosition: 'bottom',
        swipeEnabled: false,
        headerTintColor: '#fff',
        tabBarOptions: {
            activeTintColor: '#2395ff',
            inactiveBackgroundColor: Constant.TEXT_COLOR_F8,
            activeBackgroundColor: Constant.TEXT_COLOR_F8,
            style: {
                shadowColor: Constant.TEXT_COLOR_9,
                shadowOffset: {h: -2, w: .5},
                shadowOpacity: 0.5,
            },
            labelStyle: {
                fontSize: 10
            }
        }
    }
);


function getCurrentRouteName(navigationState) {
    if (!navigationState) {
        return null;
    }
    const route = navigationState.routes[navigationState.index];
    if (route.routes) {
        return getCurrentRouteName(route);
    }
    return route.routeName;
}

const lightContentScenes = ['MainTab', 'WaterShop', 'Login'];

class App extends Component {

    constructor(props) {
        super(props)
        StatusBar.setBarStyle('light-content')
    }

    componentDidMount() {
        loginStore.user().then(result=>{
            if(!result){
                RequestUtil.requestData('login/tourist', {}).then((resp) => {
                    if (resp == undefined) return;
                    LOG('游客登陆App' + JSON.stringify(resp));
                    loginStore.save('',resp.access_token,resp.access_secret);
                })
            }
        })
    }

    render() {
        return (
            <Navigator
                onNavigationStateChange={
                    (prevState, currentState) => {
                        const currentScene = getCurrentRouteName(currentState);
                        const previousScene = getCurrentRouteName(prevState);
                        if (previousScene !== currentScene) {
                            if (lightContentScenes.indexOf(currentScene) >= 0) {
                                StatusBar.setBarStyle('light-content')
                            } else {
                                StatusBar.setBarStyle('dark-content')
                            }
                        }
                    }
                }
            />
        );
    }
}


const Navigator = StackNavigator({
        Root: {
            screen: TabNav,
        },
        WaterShop: {
            screen: WaterShop
        },
        TicketDetails: {
            screen: TicketDetails
        },
        ProductDetails: {
            screen: ProductDetails
        },
        OrderDetail: {
            screen: OrderDetailController
        },
        Login: {
            screen: Login,
            navigationOptions: {
                headerStyle: {
                    backgroundColor: Constant.BLUECOLOR
                }
            }
        },
        Brand: {
            screen: Brand
        },
        PersonInfo: {
            screen: PersonInfo
        },
        Setting: {
            screen: Setting
        },
        AccountMgr: {
            screen: AccountMgr
        },
        MineWaterTikets: {
            screen: MineWaterTikets
        },
        ChangePwd: {
            screen: ChangePwd
        },
        DeliveryAddress: {
            screen: DeliveryAddress
        },
        AddOrEditAddress: {
            screen: AddOrEditAddress
        },
        TiketsViable: {
            screen: TiketsViable
        },
        SearchAddress: {
            screen: SearchAddress
        },
        ConfirmOrder: {
            screen: ConfirmOrder
        },
        QuickPay: {
            screen: QuickPay
        },
        Map: {
            screen: Map
        },
        AddressList: {
            screen: AddressList
        },
        Judge: {
            screen: Judge
        },
        CityChooser: {
            screen: CityChooser
        },
        PayResult: {
            screen: PayResult
        },
        ShoppingCart: {
            screen: ShoppingCart
        },

    },
    {
        navigationOptions: {
            headerTintColor: Constant.TEXT_COLOR_6,
            headerBackTitle: '返回',
            swipeEnabled: false,
            headerStyle: {
                backgroundColor: '#fff'
            }
        },
    },
);


export default App;